<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2>登录</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
        <!-- 新增身份选择 -->
        <el-form-item prop="role">
          <el-select v-model="loginForm.role" placeholder="请选择身份">
            <el-option label="管理员" value="admin"></el-option>
            <el-option label="学生" value="student"></el-option>
            <el-option label="老师" value="teacher"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            prefix-icon="User"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            :type="passwordVisible ? 'text' : 'password'" 
            v-model="loginForm.password" 
            placeholder="请输入密码"
            prefix-icon="Lock"
          >
            <template #suffix>
              <el-icon @click="togglePasswordVisibility" style="cursor: pointer">
                <component :is="passwordVisible ? 'Hide' : 'View'"></component>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter }from'vue-router'
import { useUserStore } from '../stores/counter'


const router = useRouter()
const counterStore = useUserStore()


// 定义登录表单数据
const loginForm = ref({
  username: '',
  password: '',
  role: '' // 新增字段：身份
});

// 密码可见性状态
const passwordVisible = ref(false);

// 切换密码可见性
const togglePasswordVisibility = () => {
  passwordVisible.value = !passwordVisible.value;
};

// 定义表单验证规则
const rules = {
  role: [
    { required: true, message: '请选择身份', trigger: 'change' }
  ],
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
};

// 表单引用
const loginFormRef = ref(null);

// 登录处理函数
const handleLogin = () => {
  loginFormRef.value.validate((valid) => {
    if (valid) {
      loginForm.value.role = loginForm.value.role;
   
      ElMessage.success(`登录成功，当前身份为：${loginForm.value.role}`);

   
     counterStore.setUserInfo(loginForm.value);

    
     
      // 这里可以跳转到首页或其他页面

   

      router.push('/')

    } else {
      ElMessage.error('请输入正确的用户名和密码');
    }
  });
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('/src/assets/ppn.png'); /* 添加背景图片 */
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.login-card {
  width: 400px;
  padding: 20px;
  border-radius: 16px; /* 增加圆角 */
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2); /* 增强阴影效果 */
  background-color: rgba(255, 255, 255, 0.9); /* 半透明背景 */
}

h2 {
  text-align: center;
  margin-bottom: 20px;
  color: #333; /* 调整字体颜色 */
  font-size: 24px; /* 增大字体大小 */
}

.el-button--primary {
  width: 100%;
  font-size: 16px;
  transition: all 0.3s ease; /* 添加悬停效果 */
}

.el-button--primary:hover {
  background-color: #409eff; /* 悬停时加深颜色 */
  transform: scale(1.05); /* 悬停时放大按钮 */
}
</style>